/**
 * 
 */
package com.fox.webapp.service.impl;

import java.util.Locale;
import java.util.UUID;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import org.thymeleaf.context.IWebContext;
import org.thymeleaf.context.WebContext;

import com.fox.webapp.domain.Person;
import com.fox.webapp.persistent.PersonRepository;
import com.fox.webapp.service.IEmailService;

/**
 * @author yockii
 *
 */
public class EmailService implements IEmailService {

	private final static Logger logger = LoggerFactory.getLogger(EmailService.class);
	private final static String ENCODING = "UTF-8";
	private String from = "EconGen支持团队";

	private String welcomeEmailSubject = "感谢您注册EconGen";
	private String emailVerifySubject = "电子邮箱验证提醒";
	private String resetPwSubjet = "重置密码提醒";
	private String emailModifySubject = "邮箱更改提醒";
	
	@Autowired
	private JavaMailSender mailSender;
	@Autowired
	private TemplateEngine templateEngine;
	@Autowired
	private PersonRepository personRepository;
	
	private void doSendEmail(final Context ctx, String templateName, String subject, String toWho) throws MessagingException{
		final MimeMessage mimeMessage = mailSender.createMimeMessage();
		final MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, ENCODING);

		message.setSubject(subject);
		message.setFrom(from);
		message.setTo(toWho);
		
		final String htmlContent = templateEngine.process(templateName, ctx);
		message.setText(htmlContent, true);
		
		mailSender.send(mimeMessage);
	}
	
	@Override
	public void welcomEmail(Person person, Locale locale) throws MessagingException {
		final Context ctx = new Context(locale);
		
		String uuid = UUID.randomUUID().toString().replaceAll("-", "");
		person.setEmailVerifyCode(uuid);
		personRepository.updatePersonEmailCode(person);

		ctx.setVariable("person", person);

//		final MimeMessage mimeMessage = mailSender.createMimeMessage();
//		final MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, ENCODING);
//		
//		message.setSubject(welcomeEmailSubject);
//		message.setFrom(from);
//		message.setTo(person.getEmail());
//		
//		final String htmlContent = templateEngine.process("welcome", ctx);
//		message.setText(htmlContent, true);
//		
//		mailSender.send(mimeMessage);
		
		doSendEmail(ctx, "welcome_Email", welcomeEmailSubject, person.getEmail());
	}

	@Override
	public void errorMessageEmail(String errorMsg) throws MessagingException {
		final Context ctx = new Context();
		ctx.setVariable("error", errorMsg);
		
//		final MimeMessage mimeMessage = mailSender.createMimeMessage();
//		final MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, ENCODING);
//		
//		message.setSubject("系统信息");
//		message.setTo("xuyuqi@gmail.com");
//		
//		final String htmlContent = templateEngine.process("error", ctx);
//		message.setText(htmlContent, true);
//		
//		mailSender.send(mimeMessage);
		doSendEmail(ctx, "error_Email", "系统信息", "xuyuqi@gmail.com");
		
		logger.error(errorMsg);
	}

	@Override
	public void verifyEmail(Person person) throws MessagingException {
		final Context ctx = new Context();
		String uuid = UUID.randomUUID().toString().replaceAll("-", "");
		person.setEmailVerifyCode(uuid);
		personRepository.updatePersonEmailCode(person);

		ctx.setVariable("person", person);
//		final MimeMessage mimeMessage = mailSender.createMimeMessage();
//		final MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, ENCODING);
//
//		message.setSubject(emailVerifySubject);
//		message.setFrom(from);
//		message.setTo(person.getEmail());
//		
//		final String htmlContent = templateEngine.process("verifyEmail", ctx);
//		message.setText(htmlContent, true);
//		
//		mailSender.send(mimeMessage);
		
		doSendEmail(ctx, "verifyEmail_Email", emailVerifySubject, person.getEmail());
	}
	
	@Override
	public void sendResetPwEmail(Person person) throws MessagingException {
		final Context ctx = new Context();
		String uuid = UUID.randomUUID().toString().replaceAll("-", "");
		person.setResetPasswordCode(uuid);
		personRepository.updateResetPasswordCode(person);

		ctx.setVariable("person", person);
//		final MimeMessage mimeMessage = mailSender.createMimeMessage();
//		final MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, ENCODING);
//
//		message.setSubject(resetPwSubjet);
//		message.setFrom(from);
//		message.setTo(person.getEmail());
//		
//		final String htmlContent = templateEngine.process("resetPwEmail", ctx);
//		message.setText(htmlContent, true);
//		
//		mailSender.send(mimeMessage);
		doSendEmail(ctx, "resetPassword_Email", resetPwSubjet, person.getEmail());
	}
	
	public String getFrom() {
		return from;
	}
	
	public void setFrom(String from) {
		this.from = from;
	}
	
	public String getWelcomeEmailSubject() {
		return welcomeEmailSubject;
	}
	
	public void setWelcomeEmailSubject(String welcomeEmailSubject) {
		this.welcomeEmailSubject = welcomeEmailSubject;
	}
	
	public String getEmailVerifySubject() {
		return emailVerifySubject;
	}
	
	public void setEmailVerifySubject(String emailVerifySubject) {
		this.emailVerifySubject = emailVerifySubject;
	}

	/**
	 * @return the resetPwSubjet
	 */
	public String getResetPwSubjet() {
		return resetPwSubjet;
	}

	/**
	 * @param resetPwSubjet the resetPwSubjet to set
	 */
	public void setResetPwSubjet(String resetPwSubjet) {
		this.resetPwSubjet = resetPwSubjet;
	}

	@Override
	public void sendModifyEmailEmail(Person person) throws MessagingException {
		final Context ctx = new Context();
		String uuid = UUID.randomUUID().toString().replaceAll("-", "");
		person.setEmailModifyCode(uuid);
		personRepository.updateEmailModifyCode(person);

		ctx.setVariable("person", person);
		doSendEmail(ctx, "emailModify_Email", emailModifySubject, person.getEmail());
	}
	
}
